package com.tanjor.main.sys.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tanjor.common.datatable.PageReq;
import com.tanjor.common.datatable.PageResp;
import com.tanjor.user.dubbo.dto.SysUserDTO;
import com.tanjor.user.dubbo.impl.interfaces.ISysUserDubbo;

@Controller
@RequestMapping("/sys/user")
public class UserController {
	
	@Autowired
	private ISysUserDubbo sysUserDubbo;
	
	@RequestMapping("/list")
	public String list(Model model){
		return "manage/sys/user-list";
	}
	
	@RequestMapping("/loadPage")
	@ResponseBody
	public PageResp loadPage(PageReq pageReq){
		PageResp page = sysUserDubbo.findPage(pageReq);
		return page;
	}
	
	@RequestMapping("/goEdit/{id}")
	public String eidt(Model model,@PathVariable String id){
		model.addAttribute("id", id);
		return "manage/sys/user-edit";
	}
	
	@RequestMapping("/goAdd")
	public String add(){
		return "manage/sys/user-edit";
	}
	
	@RequestMapping("/changePwd/{id}")
	public String changePwd(Model model,@PathVariable String id){
		model.addAttribute("id", id);
		return "manage/sys/user-changepwd";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Map<String,Object> save(SysUserDTO user){
		if(StringUtils.isNotBlank(user.getId())){
			return sysUserDubbo.update(user);
		}else{
			return sysUserDubbo.insert(user);
		}
	}
	
	@RequestMapping("/getModel")
	@ResponseBody
	public SysUserDTO getModel(String id){
		if(StringUtils.isNotBlank(id)){
			SysUserDTO user = sysUserDubbo.findById(id);
			user.setPassword("");//避免密码泄露
			return user;
		}else{
			return new SysUserDTO();
		}
	}
	
	@RequestMapping("/userrole/{userId}")
	public String userRoles(Model model,@PathVariable String userId){
		model.addAttribute("userId", userId);
		return "/manage/sys/user-role";
	}
	
	@RequestMapping("/saveUserRole")
	@ResponseBody
	public Map<String,Object> saveUserRole(String userId,String roleCodes){		
		return sysUserDubbo.saveUserRole(userId, roleCodes);
	}
}
